package Limit.ETC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.IdFactory;
import l1j.server.server.datatables.InnKeyTable;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.L1DwarfInventory;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.templates.L1Item;
import l1j.server.server.utils.SQLUtil;

public class Warehouse_Insert{
	
	public void TransferItems(L1PcInstance pc) {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM character_warehouse_insert WHERE account_name = ?");
			pstm.setString(1, pc.getAccountName());

			rs = pstm.executeQuery();

			while (rs.next()) {
				L1ItemInstance item = new L1ItemInstance();
				int objectId = IdFactory.getInstance().nextId();
				item.setId(objectId);
				L1Item itemTemplate = ItemTable.getInstance().getTemplate(rs.getInt("item_id"));
				item.setItem(itemTemplate);
				item.setCount(rs.getInt("count"));
				item.setEquipped(false);
				item.setEnchantLevel(rs.getInt("enchantlvl"));
				item.setIdentified(rs.getInt("is_id") != 0 ? true : false);
				item.set_durability(rs.getInt("durability"));
				item.setChargeCount(rs.getInt("charge_count"));
				item.setRemainingTime(rs.getInt("remaining_time"));
				item.setLastUsed(rs.getTimestamp("last_used"));
				item.setBless(rs.getInt("bless"));
				item.setAttrEnchantKind(rs.getInt("attr_enchant_kind"));
				item.setAttrEnchantLevel(rs.getInt("attr_enchant_level"));
				item.setFireMr(rs.getInt("firemr"));
				item.setWaterMr(rs.getInt("watermr"));
				item.setEarthMr(rs.getInt("earthmr"));
				item.setWindMr(rs.getInt("windmr"));
				item.setaddSp(rs.getInt("addsp"));
				item.setaddHp(rs.getInt("addhp"));
				item.setaddMp(rs.getInt("addmp"));
				item.setHpr(rs.getInt("hpr"));
				item.setMpr(rs.getInt("mpr"));
				item.setM_Def(rs.getInt("m_def"));
				item.setTakiWeapon(rs.getInt("takiweapon"));// 覺醒系統(武) 0724
				item.setTakiArmor(rs.getInt("takiarmor"));// 覺醒系統(防) 0724
				item.setDamageReduction(rs.getInt("damagereduction"));
				item.setMagicReduction(rs.getInt("magicreduction"));
				item.setDoubleDmgChance(rs.getInt("doubledmgchance"));
				item.setMagicDmgModifier(rs.getInt("magicdmgmodifier"));
				item.setDmgModifier(rs.getInt("dmgmodifier"));
				item.setBowDmgModifier(rs.getInt("bowdmgmodifier"));
				item.setHitModifier(rs.getInt("hitmodifier"));
				item.setBowHitModifier(rs.getInt("bowhitmodifier"));
				item.setRegisStun(rs.getInt("regisstun"));
				item.setRegisStone(rs.getInt("regisstone"));
				item.setRegisFreeze(rs.getInt("regisfreeze"));
				item.setRegisSustain(rs.getInt("regissustain"));
				item.setJewelryType1(rs.getInt("jewelrytype1"));
				item.setJewelryLv1(rs.getInt("jewelrylv1"));	
				item.setJewelryType2(rs.getInt("jewelrytype2"));	
				item.setJewelryLv2(rs.getInt("jewelrylv2"));	
				item.setJewelryUse(rs.getInt("jewelryuse"));	
				item.setEnchantCount(rs.getInt("enchantcount"));
				// 登入鑰匙紀錄
				if (item.getItem().getItemId() == 40312) {
					InnKeyTable.checkey(item);
				}
				
				L1World.getInstance().storeObject(item);
				L1DwarfInventory storage = new L1DwarfInventory(pc);
				
				storage.insertItem(item);
				insertItemToLog(item, pc);
				deleteItem(item, pc);
			}

		} catch (SQLException e) {

		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void insertItemToLog(L1ItemInstance item, L1PcInstance pc) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("INSERT INTO character_warehouse_insert_log SET id = ?, account_name = ?, item_id = ?, item_name = ?, count = ?, is_equipped=0, enchantlvl = ?, is_id = ?, durability = ?, charge_count = ?, remaining_time = ?, last_used = ?, bless = ?, attr_enchant_kind = ?, attr_enchant_level = ?, firemr = ?,watermr = ?,earthmr = ?,windmr = ?,addsp = ?,addhp = ?,addmp = ?,hpr = ?,mpr = ?,m_def = ?, TakiWeapon = ?, TakiArmor = ?, damagereduction=?, magicreduction =?, doubledmgchance=?, magicdmgmodifier =?, dmgmodifier =?, bowdmgmodifier = ?, hitmodifier =?, bowhitmodifier =?, regisstun =?, regisstone =?, regisfreeze =?, regissustain =?, jewelrytype1=?, jewelrylv1=?, jewelrytype2=?, jewelrylv2=?, jewelryuse=?, enchantcount=?");
			pstm.setInt(1, item.getId());
			pstm.setString(2, pc.getAccountName());
			pstm.setInt(3, item.getItemId());
			pstm.setString(4, item.getName());
			pstm.setInt(5, item.getCount());
			pstm.setInt(6, item.getEnchantLevel());
			pstm.setInt(7, item.isIdentified() ? 1 : 0);
			pstm.setInt(8, item.get_durability());
			pstm.setInt(9, item.getChargeCount());
			pstm.setInt(10, item.getRemainingTime());
			pstm.setTimestamp(11, item.getLastUsed());
			pstm.setInt(12, item.getBless());
			pstm.setInt(13, item.getAttrEnchantKind());
			pstm.setInt(14, item.getAttrEnchantLevel());
			pstm.setInt(15, item.getFireMr());
			pstm.setInt(16, item.getWaterMr());
			pstm.setInt(17, item.getEarthMr());
			pstm.setInt(18, item.getWindMr());
			pstm.setInt(19, item.getaddSp());
			pstm.setInt(20, item.getaddHp());
			pstm.setInt(21, item.getaddMp());
			pstm.setInt(22, item.getHpr());
			pstm.setInt(23, item.getMpr());
			pstm.setInt(24, item.getM_Def());
			pstm.setInt(25, item.getTakiWeapon());// 覺醒系統(武) 0724
			pstm.setInt(26, item.getTakiArmor());// 覺醒系統(防) 0724
			pstm.setInt(27, item.getDamageReduction());
			pstm.setInt(28, item.getMagicReduction());
			pstm.setInt(29, item.getDoubleDmgChance());
			pstm.setInt(30, item.getMagicDmgModifier());
			pstm.setInt(31, item.getDmgModifier());
			pstm.setInt(32, item.getBowDmgModifier());
			pstm.setInt(33, item.getHitModifier());
			pstm.setInt(34, item.getBowHitModifier());
			pstm.setInt(35, item.getRegisStun());
			pstm.setInt(36, item.getRegisStone());
			pstm.setInt(37, item.getRegisFreeze());
			pstm.setInt(38, item.getRegisSustain());
			pstm.setInt(39, item.getJewelryType1());
			pstm.setInt(40, item.getJewelryLv1());
			pstm.setInt(41, item.getJewelryType2());
			pstm.setInt(42, item.getJewelryLv2());
			pstm.setInt(43, item.getJewelryUse());
			pstm.setInt(44, item.getEnchantCount());
			pstm.execute();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}

	}
	
	public void deleteItem(L1ItemInstance item, L1PcInstance pc) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("DELETE FROM character_warehouse_insert WHERE item_id = ? AND account_name =? ");
			pstm.setInt(1, item.getItemId());
			pstm.setString(2, pc.getAccountName());
			pstm.execute();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public boolean isExist(L1PcInstance pc) {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM character_warehouse WHERE account_name = ?");
			pstm.setString(1, pc.getAccountName());
			rs = pstm.executeQuery();

			while (rs.next()) {
				return true;
			}

		} catch (SQLException e) {

		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return false;
	}
}
